package com.cm.res.scanner;

import org.jetbrains.annotations.NotNull;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Set;

public class MainEntry {

    static final SimpleDateFormat SDF = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    public static void main(String[] args) {

        if (args == null) return;

        if (args.length == 0) {
            System.err.println("please input  project root path");
            return;
        }
        String rootPath = getWorkDirName(args[0]);

        String[] resourceTypes = new String[args.length - 1];
        if (args.length <= 1) {
            System.err.println("please input resource type");
            return;
        }
        for (int i = 1; i < args.length; i++) {
            resourceTypes[i - 1] = args[i];
        }

        long startCheckTime = System.currentTimeMillis();
        System.out.println("RootPath: " + rootPath);

        ResourceChecker checker = ResourceChecker.getInstance();
        checker.deleteResultFile(rootPath);
        checker.startCheck(rootPath, resourceTypes);

        Set<String> resources = checker.waitOnUntilFinish();

        if (resources != null) {
            checker.saveToResultFile(resources, rootPath);
        }

        long endCheckTime = System.currentTimeMillis();
        long dx = endCheckTime - startCheckTime;

        System.err.println("start time: " + SDF.format(startCheckTime));
        System.err.println("end time: " + SDF.format(endCheckTime));
        System.err.println("const time: " + dx);

    }

    @NotNull
    private static String getWorkDirName(String arg) {
        String rootPath = arg;

        File f = new File(rootPath);
        rootPath = f.getAbsolutePath();

        if (rootPath.endsWith(File.separator + ".")) {
            rootPath = rootPath.substring(0, rootPath.length() - 2);
        } else if (rootPath.endsWith(File.separator + "..")) {
            rootPath = rootPath.substring(0, rootPath.length() - 3);
            int lastIndex = rootPath.lastIndexOf(File.separator);
            if (lastIndex >= 0) {
                rootPath = rootPath.substring(0, lastIndex);
            }
        }
        return rootPath;
    }
}
